package com.kyxx.mycommonproject.lunbotu;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.kyxx.mycommonproject.R;
import com.kyxx.mycommonproject.base.BaseFragment;
import com.kyxx.mycommonproject.lunbotu.adapter.CircleIndicator;
import com.kyxx.mycommonproject.lunbotu.adapter.LoopViewPager;
import com.kyxx.mycommonproject.lunbotu.adapter.PicAdapter;
import com.kyxx.mycommonproject.lunbotu.model.bean.CarouselfigureBean;
import com.kyxx.mycommonproject.lunbotu.presenter.CarouselfigurePresenter;
import com.kyxx.mycommonproject.lunbotu.view.ICarouselfigureView;

/**
 * Created by GQI on 2016/9/8.
 */
public class CarouselfigureFragment extends BaseFragment implements ICarouselfigureView{
    private View view;
    private LoopViewPager mLooperViewPager;
    private Context ctx;
    private CircleIndicator indicator;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_carousel_figure,container,false);
        ctx = getActivity();
        /** viewpager主体*/
        mLooperViewPager = (LoopViewPager) view.findViewById(R.id.viewpager);
        /** 轮播图显示的标点*/
        indicator = (CircleIndicator) view.findViewById(R.id.indicator);
        CarouselfigurePresenter presenter = new CarouselfigurePresenter(this);
        presenter.init();
        return view;
    }
    private LoopViewPager.OnDispatchTouchEventListener mDispatchOnTouchListener = new LoopViewPager.OnDispatchTouchEventListener() {
        @Override
        public void onDispatchKeyEvent(MotionEvent event) {
//            Log.d(TAG, "mDispatchOnTouchListener.onDispatchKeyEvent(" + event + ")");
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mLooperViewPager.setLooperPic(false);
            } else if (event.getAction() == MotionEvent.ACTION_UP
                    || event.getAction() == MotionEvent.ACTION_CANCEL) {
                mLooperViewPager.setLooperPic(true);
            }
        }
    };
    /** 初始化数据 显示轮播图图片或简介*/
    @Override
    public void initData(CarouselfigureBean beans) {
        mLooperViewPager.setOnDispatchTouchEventListener(mDispatchOnTouchListener);
        mLooperViewPager.setLooperPic(true);
        mLooperViewPager.setAdapter(new PicAdapter(beans,ctx));
        indicator.setViewPager(mLooperViewPager);
    }

}
